package com.household.controller;

import com.household.constant.SecurityConstant;
import com.household.domain.ResponseResult;
import com.household.domain.entity.User;
import com.household.domain.enums.ResponseErrorCodeEnum;
import com.household.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created By wanzhen
 * 2023. 2023/3/25. 14:59
 */

@Slf4j
@Controller
public class LoginController {

    private final UserService userService;

    @Autowired
    public LoginController(UserService userService) {
        this.userService = userService;
    }


    @RequestMapping(SecurityConstant.LOGIN_URL)
    public String login(){
        System.out.println("Login...");
        log.info("Login...");
        return "login";
    }

    @RequestMapping(SecurityConstant.LOGIN_TO_URL)
    public void showSuccessPage(@AuthenticationPrincipal User user, HttpServletResponse response, HttpSession session) throws IOException {
        switch (user.getRole()){
            case DIS_FORMAL:
                response.sendRedirect("/completeInfo");
                return;
            case ADMIN:
                response.sendRedirect("/admin/dashboard");
                return;
            case USER:
                response.sendRedirect("/user/index");
                return;
            case COMPANY:
                response.sendRedirect("/company/index");
                return;
            default:
                session.setAttribute(SecurityConstant.LAST_EXCEPTION, ResponseResult.failure(ResponseErrorCodeEnum.NO_PERMISSION));
                response.sendRedirect(SecurityConstant.UN_AUTHENTICATION_URL);
        }
    }
    @RequestMapping(SecurityConstant.UN_AUTHENTICATION_URL)
    public String showAuthenticationPage(ModelMap map, HttpSession session){
        Object exception = session.getAttribute(SecurityConstant.LAST_EXCEPTION);
        if(exception != null) {
            if(exception instanceof BadCredentialsException) {
                map.put("message", ResponseErrorCodeEnum.PASSWORD_ERROR.getMsg());
            } else if (exception instanceof AuthenticationException){
                map.put("message", ((AuthenticationException)exception).getMessage());
            } else if (exception instanceof ResponseResult) {
                map.put("message", ((ResponseResult) exception).getMsg());
            } else if (exception instanceof String) {
                map.put("message", exception);
            }
        }

        session.removeAttribute(SecurityConstant.LAST_EXCEPTION);
        return "login";

    }

    /**
     * 跳转到信息补全页面
     * @author jitwxs
     * @date 2019/4/21 22:14
     */
    @RequestMapping("/completeInfo")
    public String showCompleteInfoPage(){
        return "completeInfo";
    }

    @RequestMapping("/register")
    public String registerPage(){
        log.info("用户注册！");
        return "register";
    }




}
